package com.ruoyi.StarStudio.Controller;

import com.ruoyi.StarStudio.Domain.*;
import com.ruoyi.StarStudio.Service.WarningService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;

@RestController
@RequestMapping("/warning")
public class WarningController {

    @Autowired
    private WarningService warningService;

    /**
     * 获取所有预警数据
     * @return
     */
    @GetMapping
    public Result getAll(){
        List<Warning> warningList = null;
        try {
            warningList = warningService.getAll();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.WARNING_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.WARNING_QUERY_SUCCESS, warningList);
    }

    /**
     * 根据地块id获取一条预警数据
     * @param zone_id
     * @return
     */
    @GetMapping("/{zone_id}")
    public Result getByZoneId(@PathVariable Integer zone_id){
        Warning warningByZoneId = null;
        try {
            warningByZoneId = warningService.getByZoneId(zone_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.WARNING_QUERY_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.WARNING_QUERY_SUCCESS, warningByZoneId);
    }

    /**
     * 修改预警数据
     * @param warning
     * @return
     */
    @PutMapping
    public Result update(@RequestBody Warning warning){
        try {
            //获取当前时间戳
            Timestamp timestamp = new Timestamp(System.currentTimeMillis());
            warning.setWarning_updatetime(timestamp);
            warningService.update(warning);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR, MessageConstant.WARNING_UPDATE_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS, MessageConstant.WARNING_UPDATE_SUCCESS);
    }

}
